﻿@using BV.PACS.WEB.Shared.Models
@using BV.PACS.WEB.Client.Services.Api
@using BV.PACS.WEB.Client.Shared.Components
@inherits BV.PACS.WEB.Client.Materials.BatchContentGridPanelCode



@if (DataSource != null && DataSource.Count > 0)
{
    <DxFormLayout CaptionPosition="@CaptionPosition.Vertical">
        <DxFormLayoutItem ColSpanLg="12" ColSpanMd="12">
            <Template Context="GridContext">
                <DxDataGrid Data="@DataSource"
                            ShowFilterRow="@false"
                            ShowPager="@false"
                            AllowDataRowSelection="@true"
                            PageSize="@BaseSettings.MaxBatchRegistrationSize"
                            RowUpdating=@((updatingDataItem, newValues) => OnRowUpdating(updatingDataItem, newValues))>

                    <DxDataGridCommandColumn Width="150px"></DxDataGridCommandColumn>
                    <DxDataGridColumn Field="@nameof(BatchRegistrationDto.SourceBarcode)" Caption="@Translations.SourceBarcode">

                    </DxDataGridColumn>

                    <DxDataGridComboBoxColumn Data="@SourceTemplates" Field="@nameof(BatchRegistrationDto.SourceTemplate)" Caption="@Translations.SourceTemplate">                      
                    </DxDataGridComboBoxColumn>


                    <DxDataGridColumn Field="@nameof(BatchRegistrationDto.SourceTypeId)" Caption="@Translations.SourceType">
                        <DisplayTemplate>
                            @{
                                var sourceTypeContext = context as BatchRegistrationDto;
                                <span>@sourceTypeContext?.SourceType</span>
                            }
                        </DisplayTemplate>

                        <EditTemplate>
                            @{ 
                                var sourceCellContext = (CellEditContext)context; 
                                string sourceTypeId = (string)sourceCellContext.CellValue;
                                var sourceTypeItem = SourceTypes.FirstOrDefault(t => t.Id == sourceTypeId) ?? new SourceMaterialTypeLookupItem();
                            }
                            <SourceMaterialTree Data="@SourceTypes"
                                                SelectedItem="@sourceTypeItem"
                                                OnSelect=@(newCellValue => {sourceCellContext.OnChanged(newCellValue);})>

                            </SourceMaterialTree>
                        </EditTemplate>
                    </DxDataGridColumn>

                    <DxDataGridColumn Field="@nameof(BatchRegistrationDto.MaterialBarcode)" Caption="@Translations.MaterialBarcode">
                    </DxDataGridColumn>

                    <DxDataGridComboBoxColumn Data="@MaterialTemplates" Field="@nameof(BatchRegistrationDto.MaterialTemplate)" Caption="@Translations.MaterialTemplate">
                    </DxDataGridComboBoxColumn>

                    <DxDataGridColumn Field="@nameof(BatchRegistrationDto.MaterialTypeId)" Caption="@Translations.MaterialType">
                        <DisplayTemplate>
                            @{
                                var materialTypeContext = context as BatchRegistrationDto;
                                <span>@materialTypeContext?.MaterialType</span>
                            }
                        </DisplayTemplate>

                        <EditTemplate>
                            @{ 
                                var materialCellContext = (CellEditContext)context; 
                                string materialTypeId = (string)materialCellContext.CellValue;
                                var materialTypeItem = MaterialTypes.FirstOrDefault(t => t.Id == materialTypeId) ?? new SourceMaterialTypeLookupItem();
                            }
                            <SourceMaterialTree Data="@MaterialTypes"
                                                SelectedItem="@materialTypeItem"
                                                OnSelect=@(newCellValue => {materialCellContext.OnChanged(newCellValue);})>

                            </SourceMaterialTree>
                        </EditTemplate>
                    </DxDataGridColumn>

                    <DxDataGridColumn Field="@nameof(BatchRegistrationDto.AliquotBarcode)" Caption="@Translations.AliquotBarcode">
                    </DxDataGridColumn>

                    <DxDataGridComboBoxColumn Data="@AliquotTemplates" Field="@nameof(BatchRegistrationDto.AliquotTemplate)" Caption="@Translations.AliquotTemplate">
                    </DxDataGridComboBoxColumn>

                    <DxDataGridColumn Field="@nameof(BatchRegistrationDto.AliquotLocation)" Caption="@Translations.AliquotLocation">
                    </DxDataGridColumn>
                </DxDataGrid>

            </Template>
        </DxFormLayoutItem>
    </DxFormLayout>
}